
import loginConfig from "@/assets/login/config";
// import store from '@/store';
export function getContentPath(){
  return window.location.pathname.split('/')[1];
}
export function login (){
  var httpRequest = new XMLHttpRequest();
  var token = window.location.search.split('=')[1];
    httpRequest.open('GET', loginConfig.sso_validate_url +"?ticket="+token+"&appKey="+loginConfig[getContentPath()+"_appKey"], true);
    httpRequest.send();
    /**
     * 获取数据后的处理程序
     */
    httpRequest.onreadystatechange = function () {
        if (httpRequest.readyState == 4 && httpRequest.status == 200) {
            var json = httpRequest.responseText;//获取到json字符串，还需解析
            var r = JSON.parse(json);
            if(r && r.logined){
              localStorage.setItem('userinfo',json);
              localStorage.setItem('token', token);
              var newurl = (window.location.href + '').split('&ticket');
              if (newurl.length > 1) {
                window.location.href = newurl[0] + '#' + newurl[1].split('#')[1] + "dashboard";
              }
            } else{
              logout();
            }
        }
    };
}
export function logout () {
  /**
   *  执行系统退出    一定跳转到登录界面  实现真实的退出业务逻辑  
   */
  // window.hystore.clearLoginScope();
  var loginBeforeTargteUrl = location.href;
  var logoutUrl = loginConfig.sso_logout_url + "?service=" + loginConfig.sso_login_url + "?service=" +loginBeforeTargteUrl;
  window.location.href = logoutUrl;
  localStorage.removeItem("AuthButtons");
  localStorage.clear();
}